#include<bits/stdc++.h>
using namespace std;
const int inf=0x3f3f3f3f;
const int maxn=2005;
int g[maxn][maxn],Cnt[201],dis[201],m,n,x,y,z,flow,ans,i;
int dfs(int u,int flow){
	int v,temp,delta;
	if(u==n) return flow;
	delta=false;
	for(v=1;v<=n;v++){
		if(g[u][v]>0&&dis[u]==dis[v]+1){
			temp=dfs(v,min(flow-delta,g[u][v]));
			g[u][v]-=temp;
			g[v][u]+=temp;
			delta+=temp;
			if(delta==flow||dis[1]>=n) return delta;
		}
	}
	if(dis[1]>=n) return delta;
	Cnt[dis[u]]--;
	if(Cnt[dis[u]]==0) dis[1]=n;;
	Cnt[dis[u]++]++;
	return delta;
}
inline int read(int &x){
	scanf("%d",&x);
}
int main(){
	read(m),read(n);
	for(i=1;i<=m;i++){
		read(x),read(y),read(z);
		g[x][y]+=z;
	}
	do{
		flow=dfs(1,inf);
		ans+=flow;
	}while(dis[1]<n);
	printf("%d\n",ans);
	return 0;
}